package com.yh.ldzl.service.impl;

import java.util.List;
import java.util.Map;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yh.ldzl.mapper.QcIpqcMapper;
import com.yh.ldzl.domain.QcIpqc;
import com.yh.ldzl.service.IQcIpqcService;
import com.yh.ldzl.service.QualityCheckNotificationService;

/**
 * 过程检验单Service业务层处理
 * 
 * @author yanghuai
 * @date 2025-07-07
 */
@Service
public class QcIpqcServiceImpl implements IQcIpqcService 
{
    @Autowired
    private QcIpqcMapper qcIpqcMapper;

    @Autowired
    private QualityCheckNotificationService qualityCheckNotificationService;

    /**
     * 查询过程检验单
     * 
     * @param ipqcId 过程检验单主键
     * @return 过程检验单
     */
    @Override
    public QcIpqc selectQcIpqcByIpqcId(Long ipqcId)
    {
        return qcIpqcMapper.selectQcIpqcByIpqcId(ipqcId);
    }

    /**
     * 查询过程检验单列表
     * 
     * @param qcIpqc 过程检验单
     * @return 过程检验单
     */
    @Override
    public List<QcIpqc> selectQcIpqcList(QcIpqc qcIpqc)
    {
        return qcIpqcMapper.selectQcIpqcList(qcIpqc);
    }

    /**
     * 新增过程检验单
     * 
     * @param qcIpqc 过程检验单
     * @return 结果
     */
    @Override
    public int insertQcIpqc(QcIpqc qcIpqc)
    {
        qcIpqc.setCreateBy(SecurityUtils.getUsername());
        return qcIpqcMapper.insertQcIpqc(qcIpqc);
    }

    /**
     * 修改过程检验单
     *
     * @param qcIpqc 过程检验单
     * @return 结果
     */
    @Override
    public int updateQcIpqc(QcIpqc qcIpqc)
    {
        qcIpqc.setUpdateBy(SecurityUtils.getUsername());
        int result = qcIpqcMapper.updateQcIpqc(qcIpqc);

        // 如果更新成功且质检结果已确定，则通知生产模块
        if (result > 0 && qcIpqc.getCheckResult() != null &&
            ("1".equals(qcIpqc.getCheckResult()) || "2".equals(qcIpqc.getCheckResult()))) {

            try {
                // 从来源信息中提取任务ID
                Long taskId = qualityCheckNotificationService.extractTaskIdFromSource(
                    qcIpqc.getSourceDocId(),
                    qcIpqc.getSourceDocType()
                );

                if (taskId != null) {
                    System.out.println("质检完成，通知生产模块 - 任务ID: " + taskId +
                                     ", 检验单ID: " + qcIpqc.getIpqcId() +
                                     ", 结果: " + qcIpqc.getCheckResult());

                    // 发送质检完成通知，添加质检类型
                    qualityCheckNotificationService.notifyProductionModule(
                        taskId,
                        qcIpqc.getIpqcId(),
                        qcIpqc.getCheckResult(),
                        qcIpqc.getQuantityQualifiedNum(),
                        qcIpqc.getQuantityUnqualifiedNum(),
                        qcIpqc.getRemark(),
                        qcIpqc.getSourceDocType(),  // 源单据类型
                        "1"  // 质检类型, 1=IPQC过程检验
                    );
                }
            } catch (Exception e) {
                System.err.println("发送质检完成通知失败: " + e.getMessage());
                e.printStackTrace();
            }
        }

        return result;
    }

    /**
     * 批量删除过程检验单
     * 
     * @param ipqcIds 需要删除的过程检验单主键
     * @return 结果
     */
    @Override
    public int deleteQcIpqcByIpqcIds(Long[] ipqcIds)
    {
        return qcIpqcMapper.deleteQcIpqcByIpqcIds(ipqcIds);
    }

    /**
     * 删除过程检验单信息
     * 
     * @param ipqcId 过程检验单主键
     * @return 结果
     */
    @Override
    public int deleteQcIpqcByIpqcId(Long ipqcId)
    {
        return qcIpqcMapper.deleteQcIpqcByIpqcId(ipqcId);
    }

    /**
     * 校验过程检验单编号是否唯一
     *
     * @param ipqcCode
     * @return
     */
    @Override
    public boolean selectQcIpqcCodeUnique(String ipqcCode) {
        return qcIpqcMapper.selectQcIpqcCodeUnique(ipqcCode) == 0;
    }

    /**
     * 校验过程检验单名称是否唯一
     *
     * @param ipqcName
     * @return
     */
    @Override
    public boolean selectQcIpqcNameUnique(String ipqcName) {
        return  qcIpqcMapper.selectQcIpqcNameUnique(ipqcName) == 0;
    }

    @Override
    public QcIpqc selectQcIpqcSourceDocId(Long workOrderId) {
        return qcIpqcMapper.selectQcIpqcSourceDocId(workOrderId);
    }

    @Override
    public Map<String, Object> selectQcIpqcProductInfo(Long work_order_id) {
        return qcIpqcMapper.selectQcIpqcProductInfo(work_order_id);
    }
}
